from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field


class ProductCategoryBase(BaseModel):
    """商品分类基础模式"""
    name: str = Field(..., max_length=100, description="分类名称")
    description: Optional[str] = Field(None, description="分类描述")
    parent_id: Optional[int] = Field(None, description="父分类ID，支持层级分类")
    sort_order: int = Field(default=0, description="排序顺序")
    is_active: int = Field(default=1, description="是否激活：1-激活，0-禁用")


class ProductCategoryCreate(ProductCategoryBase):
    """创建商品分类模式"""
    pass


class ProductCategoryUpdate(BaseModel):
    """更新商品分类模式"""
    name: Optional[str] = Field(None, max_length=100, description="分类名称")
    description: Optional[str] = Field(None, description="分类描述")
    parent_id: Optional[int] = Field(None, description="父分类ID，支持层级分类")
    sort_order: Optional[int] = Field(None, description="排序顺序")
    is_active: Optional[int] = Field(None, description="是否激活：1-激活，0-禁用")


class ProductCategoryResponse(ProductCategoryBase):
    """商品分类响应模式"""
    id: int
    created_at: datetime
    updated_at: datetime
    children: Optional[List['ProductCategoryResponse']] = None

    class Config:
        from_attributes = True


# 解决前向引用问题
ProductCategoryResponse.model_rebuild()